﻿using System.Collections.Generic;
/*
 * @Author: ws.s 
 * @Date: 2021-10-16 14:49:04 
 * @Last Modified by: ws.s
 * @Last Modified time: 2021-10-16 14:55:43
 */

using System.Collections;
using System.Text;
using System;
using UnityEngine;
using System.Linq;

namespace WssUtils
{
    /// <summary>
    /// 数组数据操作的工具
    /// </summary>
    public static class ArrayTool
    {

        /// <summary>
        /// 将一个数组按固定大小拆分成 多个
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="divideCount">划分</param>
        /// <typeparam name="T">数组类型</typeparam>
        /// <returns></returns>
        public static T[][] DivideArray<T>(T[] arr, int divideCount = 5)
        {
            int count = arr.Length % divideCount == 0 ? arr.Length / divideCount : arr.Length / divideCount + 1;
            List<T[]> list = new List<T[]>();
            for (int i = 0; i < count; i++)
            {
                int index = i * divideCount;
                T[] subarr = arr.Skip(index).Take(divideCount).ToArray();
                list.Add(subarr);
            }
            return list.ToArray();
        }

        public static T[][] DeepCopyArray2D<T>(T[][] arr)
        {
            T[][] clone = new T[arr.Length][];
            for (int i = 0; i < clone.Length; i++)
            {
                clone[i] = new T[arr[i].Length];
                for (int j = 0; j < clone.Length; j++)
                {
                    clone[i][j] = arr[i][j];
                }
            }
            return clone;
        }


        public static void Exchange<T>(ref T[] array, int a, int b)
        {
            T temp = array[a];
            array[a] = array[b];
            array[b] = temp;
        }


        /// <summary>
        /// 上下翻转二维数组(原数组数据)
        /// </summary>
        /// <param name="array2d"></param>
        /// <returns></returns>
        public static void Flip2D<T>(T[][] array2d)
        {
            Array.Reverse(array2d);
        }

        /// <summary>
        /// 左右镜像二维数组(原数组数据)
        /// </summary>
        /// <param name="array2d"></param>
        /// <returns></returns>
        public static void Mirror2D<T>(T[][] array2d)
        {
            for (int i = 0; i < array2d.Length; i++)
            {
                Array.Reverse(array2d[i]);
            }
        }

        public static void LogArray2D<T>(T[][] array2d)
        {
            Debug.Log("__________________\n");
            for (int i = 0; i < array2d.Length; i++)
            {
                Debug.Log(String.Join(",", array2d[i]) + "\n");
            }
        }


        /// <summary>
        ///  旋转二维数组(会改变数组结构)
        /// </summary>
        /// <param name="array2d"></param>
        /// <param name="angle">取值范围 0,90,180,270</param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T[][] Rotate2D<T>(T[][] a, int deg = 0)
        {
            if (deg % 90 != 0)
            {
                Debug.LogError("Invalid input; degrees must be a multiple of 90");
                return a;
            }

            int d = (deg % 360 + 360) % 360;

            if (d == 90)
            {
                return Turn90d(a);
            }
            else if (d == 180)
            {
                return Turn180d(a);
            }
            else if (d == 270)
            {
                return Turn270d(a);
            }

            //3214,4231,1273
            return a;
        }

        /// <summary>
        /// 旋转90度
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        private static T[][] Turn90d<T>(T[][] a)
        {
            int w = a.Length;
            int h = a[0].Length;
            var b = new T[h][];

            for (int y = 0; y < h; y++)
            {
                b[y] = new T[w];
                for (int x = 0; x < w; x++)
                {
                    b[y][x] = a[w - 1 - x][y];
                }
            }

            return b;
        }

        private static T[][] Turn180d<T>(T[][] a)
        {
            int w = a[0].Length;
            int h = a.Length;
            var b = new T[h][];

            for (int y = 0; y < h; y++)
            {
                int n = h - 1 - y;
                b[n] = new T[w];
                for (int x = 0; x < w; x++)
                {
                    b[n][w - 1 - x] = a[y][x];
                }
            }
            return b;
        }

        private static T[][] Turn270d<T>(T[][] a)
        {
            int w = a.Length;
            int h = a[0].Length;
            var b = new T[h][];
            for (int y = 0; y < h; y++)
            {
                b[y] = new T[w];
                for (int x = 0; x < w; x++)
                {
                    b[y][x] = a[x][h - 1 - y];
                }

            }
            return b;
        }


    }

}
